package com.example.ycdyus.dailyfruit.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.QiangxianAdapter;
import com.example.ycdyus.dailyfruit.bean.Products;
import com.example.ycdyus.dailyfruit.bean.QiangxianBean;
import com.example.ycdyus.dailyfruit.constant.URLConstant;
import com.example.ycdyus.dailyfruit.utils.ImageLoaderHelper;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class QiangxianActivity extends AppCompatActivity {
    private PullToRefreshListView homeQiangxianPulltorefresh;
    private DisplayImageOptions options;
    private QiangxianAdapter adapter;
    private List<Products> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiangxian);
        options = ImageLoaderHelper.getDisplayImageLoader();
        initView();
        initData();
    }

    private void initView() {
        homeQiangxianPulltorefresh = (PullToRefreshListView) findViewById(R.id.home_qiangxian_pulltorefresh);
    }

    private RequestParams getRequestParams(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign","5ab8ba8167c096e2dad97fb85bf496e4");
        params.addBodyParameter("sort","0");
        params.addBodyParameter("timestamp","1445775800");
        params.addBodyParameter("platform","ANDROID");
        params.addBodyParameter("source","app");
        params.addBodyParameter("page_type","7");
        params.addBodyParameter("connect_id","161c2ae532723d329ee711d284a1b36e");
        params.addBodyParameter("region_id","106092");
        params.addBodyParameter("service","product.pageListProducts");
        params.addBodyParameter("target_id","121");
        params.addBodyParameter("device_id","1b8ba62bf241cee5b8c317d07c0c503e");
        params.addBodyParameter("channel","portal");
        params.addBodyParameter("version","3.0.3");
        return params;
    }
    private void initData() {
        adapter = new QiangxianAdapter(this,list,R.layout.item_qiangxian_listview);
        homeQiangxianPulltorefresh.setAdapter(adapter);

        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, URLConstant.HOME_QIANGXAIN, getRequestParams(), new RequestCallBack<String>() {
//        httpUtils.send(HttpRequest.HttpMethod.POST, "http://ma.jilupian.me/contribution?pageSize=10&pageNo=1", getRequestParams(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if(responseInfo.result!=null){
                    QiangxianBean bean = JSONObject.parseObject(responseInfo.result,QiangxianBean.class);
                    if(bean!=null){
                        addHeadView(bean.getPage_photo());
                        addData(bean.getProducts());
                    }
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("","");
            }
        });

    }

    private void addData(List<Products> bean) {
        list.addAll(bean);
        adapter.notifyDataSetChanged();
    }

    private void addHeadView(String path) {
        ImageView iv = new ImageView(this);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        iv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,250));
        ImageLoader.getInstance().displayImage(path, iv, options);
        homeQiangxianPulltorefresh.getRefreshableView().addHeaderView(iv);
    }
}
